﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;
using zyui;

namespace zymain
{
    public partial class FrmJstj : Form
    {
        public List<string> DbKayList;
        public int _kjm;
        private string xzssb=string.Empty;
        private string bzd = string.Empty;
        private string lx = string.Empty;
        public BindingSource BookData;
        public BindingSource CfData;
        private DataTable gcdt;
        private bool all;
        private int sum = 0;
        public FrmJstj()
        {
            InitializeComponent();
        }
        public void AddDbList()
        {
            this.skKay.Items.Clear();
            this.DbKayList = new List<string>();
            this.DbKayList.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "select * from 书目数据库定义  order by 库键码";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        this.skKay.Items.Add(dRow["书目库名"].ToString().Trim());
                        this.DbKayList.Add(dRow["库键码"].ToString());
                    }
                }
                if (this.skKay.Items.Count > 0)
                {
                    this.skKay.SelectedIndex = 0;
                    this._kjm = int.Parse(this.DbKayList[0].ToString());
                }
            }
        }

        private void FrmJstj_Load(object sender, EventArgs e)
        {
            this.AddDbList();
            DateTime year = DateTime.Now.AddDays(-180);
            DateChoser.AddTo(this.sTime, year);
            DateChoser.AddTo(this.eTime);
            this.AddJstj(this._kjm);
            this.ztKay.SelectedIndex = 0;
        }

        private void skKay_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.skKay.SelectedIndex ;
            this._kjm = int.Parse(this.DbKayList[index].ToString());
            this.AddJstj(this._kjm);
        }

        private void AddJstj(int kjm)
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                this.BookData = new BindingSource();
                _open.Reset();
                _open.SqlCmd = "select * from 检索途径定义 where 库键码=" + this._kjm +" order by 所属表,检索途径类型";
                DataTable dt = _open.GetDataTable();
                foreach(DataRow dr in dt.Rows)
                {
                    dr["所属表"] = dr["所属表"].ToString().Trim();
                    dr["字段名称"] = dr["字段名称"].ToString().Trim();
                }
                this.dataGridBook.AutoGenerateColumns = false;
                this.BookData.DataSource = dt;
                this.BookData.PositionChanged += new EventHandler(this.BookData_PositionChanged);
                this.dataGridBook.DataSource = this.BookData;
                if (this.BookData.Count > 0)
                    this.plupBtn.Enabled = true;
                else
                    this.plupBtn.Enabled = false;
            }
        }

        private void BookData_PositionChanged(object sender, EventArgs e)
        {
            DataRowView current = this.BookData.Current as DataRowView;
            this.xzssb = current["所属表"].ToString().Trim();
            this.bzd = current["字段名称"].ToString().Trim();
            lx = current["类型"].ToString().Trim();
            string jslx = current["检索途径类型"].ToString().Trim();
            using (DbOperHandler _open = new Common().Doh())
            {
                this.CfData = new BindingSource();
                _open.Reset();
                string sql= "select * from 检索途径生成方式 where 检索途径类型="+ jslx+" and 类型=" + lx+" and 库键码=" + this._kjm + " and 所属表='" + this.xzssb + "'";
                if (this.xzssb.IndexOf("检索") < 0)
                    sql += " and 表字段名='" + bzd + "' order by MARC字段标识,子字段标识";
                else
                    sql += "order by MARC字段标识,子字段标识";
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                this.dataGridMdif.AutoGenerateColumns = false;
                this.CfData.DataSource = dt;
                this.dataGridMdif.DataSource = this.CfData;
                if (this.CfData.Count > 0)
                    this.upBtn.Enabled = true;
                else
                    this.upBtn.Enabled = false;
            }
        }

        private void jzdMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            int max = (int)e.Argument;
            sum = 0;
            for (int i = 0; i < max; i++)
            {
                worker.ReportProgress((int)((double)i / max * 100), "正在处理第 " + i + " 条数据，请等待...");
                string zjm = this.gcdt.Rows[i]["主键码"].ToString();
                if(this.all)
                    SaveMarc.SaveForZjm(Convert.ToInt32(zjm), this._kjm);
                else
                    SaveMarc.SaveForZjmSsb(Convert.ToInt32(zjm), this._kjm, this.xzssb, this.bzd,this.lx);
                //Console.WriteLine(zjm + ":" + this.xzssb + ":" + this._kjm+"::"+this.lx);
                Thread.Sleep(5);
                sum++;
            }
        }
        private void upBtn_Click(object sender, EventArgs e)
        {
            this.all = false;
            using (DbOperHandler _open = new Common().Doh())
            {
                if (!this.rcCheck.Checked && !this.dlhCheck.Checked)
                {
                    if (MessageBox.Show("未做任何限制，将更新所有馆藏，是否继续？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                    {
                        string sql =" select 主键码 from 馆藏书目库 where 库键码=" + this._kjm;
                        if (this.ztKay.SelectedIndex == 1)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'G')";
                        if (this.ztKay.SelectedIndex == 2)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' and 批次号 is not null)";
                        if (this.ztKay.SelectedIndex == 3)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' and 批次号 is null)";
                        if (this.ztKay.SelectedIndex == 4)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'L')";
                        sql += " order by 主键码 DESC";
                        _open.Reset();
                        _open.SqlCmd = sql;
                        this.gcdt = _open.GetDataTable();
                        if (this.gcdt.Rows.Count > 0)
                        {
                            using (ProgressHolder holder = new ProgressHolder(DoWork, "处理中..."))
                            {
                                holder.DoIt(this.gcdt.Rows.Count);
                            }
                            ShowMessageBox.ShowMessageBoxTimeout("更新【" + this.bzd + "】完成，更新" + this.gcdt.Rows.Count + "条", "提示", MessageBoxButtons.OK, 3000);
                        }
                        else
                            ShowMessageBox.ShowMessageBoxTimeout("检索结果为空，请重试...", "提示", MessageBoxButtons.OK, 3000);
                    }                        
                }
                else
                {
                    string stime = string.Empty, etime = string.Empty;
                    if (this.rcCheck.Checked)
                    {
                        stime = this.sTime.Text.Trim();
                        etime = this.eTime.Text.Trim();
                    }
                    string sql = " select 主键码 from 馆藏书目库 where 库键码=" + this._kjm;
                    if(!string.IsNullOrEmpty(stime))
                        sql += "AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and (入藏日期 BETWEEN '" + stime + "' AND '" + etime + "'))";
                    if (this.ztKay.SelectedIndex == 1)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'G')";
                    if (this.ztKay.SelectedIndex == 2)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' and 登录号 is not null)";
                    if (this.ztKay.SelectedIndex == 3)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' and 登录号 is null)";
                    if (this.ztKay.SelectedIndex == 4)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'L')";
                    string dlht = StringUtil.ToDBC(this.dlhTxt.Text.Trim());
                    if (!string.IsNullOrEmpty(dlht))
                    {
                        string sdlh = string.Empty, edlh = string.Empty;
                        if (dlht.Contains(","))
                        {
                            string[] dlhs = dlht.Split(',');
                            sdlh = dlhs[0];
                            edlh = dlhs[1];
                            if (sdlh.Length > edlh.Length)
                            {
                                edlh = sdlh.Substring(0, sdlh.Length - edlh.Length) + edlh;
                            }
                            //Console.WriteLine(sdlh + ":" + edlh);
                        }
                        else
                            sdlh = dlht;
                        if (string.IsNullOrEmpty(edlh))
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 登录号>='" + sdlh + "')";
                        else
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 登录号 BETWEEN '" + sdlh + "' and '" + edlh + "')";
                    }
                    sql += " order by 主键码 DESC";
                    //Console.WriteLine(sql);
                    _open.Reset();
                    _open.SqlCmd = sql;
                    this.gcdt = _open.GetDataTable();
                    if (this.gcdt.Rows.Count > 0)
                    {
                        using (ProgressHolder holder = new ProgressHolder(DoWork, "处理中..."))
                        {
                            holder.DoIt(this.gcdt.Rows.Count);
                        }
                        ShowMessageBox.ShowMessageBoxTimeout("更新【" + this.bzd + "】完成，更新" + this.gcdt.Rows.Count + "条","提示", MessageBoxButtons.OK, 3000);
                    }
                    else
                        ShowMessageBox.ShowMessageBoxTimeout("检索结果为空，请重试...", "提示", MessageBoxButtons.OK, 3000);
                }
            }
        }

        private void plupBtn_Click(object sender, EventArgs e)
        {
            this.all = true;
            using (DbOperHandler _open = new Common().Doh())
            {
                if (!this.rcCheck.Checked && !this.dlhCheck.Checked)
                {
                    if (MessageBox.Show("未做任何限制，将更新所有馆藏，是否继续？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                    {
                        string sql = " select 主键码 from 馆藏书目库 where 库键码=" + this._kjm;
                        if (this.ztKay.SelectedIndex == 1)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'G')";
                        if (this.ztKay.SelectedIndex == 2)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'C')";
                        if (this.ztKay.SelectedIndex == 3)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' )";
                        if (this.ztKay.SelectedIndex == 4)
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'L')";
                        sql += " order by 主键码 DESC";
                        _open.Reset();
                        _open.SqlCmd = sql;
                        this.gcdt = _open.GetDataTable();
                        if (this.gcdt.Rows.Count > 0)
                        {
                            using (ProgressHolder holder = new ProgressHolder(DoWork, "处理中..."))
                            {
                                holder.DoIt(this.gcdt.Rows.Count);
                            }
                            ShowMessageBox.ShowMessageBoxTimeout("更新【" + this.bzd + "】完成，更新" + this.gcdt.Rows.Count + "条", "提示", MessageBoxButtons.OK, 3000);
                        }
                        else
                            ShowMessageBox.ShowMessageBoxTimeout("检索结果为空，请重试...", "提示", MessageBoxButtons.OK, 3000);
                    }
                }
                else
                {
                    string stime = string.Empty, etime = string.Empty;
                    if (this.rcCheck.Checked)
                    {
                        stime = this.sTime.Text.Trim();
                        etime = this.eTime.Text.Trim();
                    }
                    string sql = " select 主键码 from 馆藏书目库 where 库键码=" + this._kjm;
                    if (!string.IsNullOrEmpty(stime))
                        sql += "AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and (入藏日期 BETWEEN '" + stime + "' AND '" + etime + "'))";
                    if (this.ztKay.SelectedIndex == 1)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'G')";
                    if (this.ztKay.SelectedIndex == 2)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'C')";
                    if (this.ztKay.SelectedIndex == 3)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' )";
                    if (this.ztKay.SelectedIndex == 4)
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'L')";
                    string dlht = StringUtil.ToDBC(this.dlhTxt.Text.Trim());
                    if (!string.IsNullOrEmpty(dlht))
                    {
                        string sdlh = string.Empty, edlh = string.Empty;
                        if (dlht.Contains(","))
                        {
                            string[] dlhs = dlht.Split(',');
                            sdlh = dlhs[0];
                            edlh = dlhs[1];
                            if (sdlh.Length > edlh.Length)
                            {
                                edlh = sdlh.Substring(0, sdlh.Length - edlh.Length) + edlh;
                            }
                            Console.WriteLine(sdlh + ":" + edlh);
                        }
                        else
                            sdlh = dlht;
                        if (string.IsNullOrEmpty(edlh))
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 登录号>='" + sdlh + "')";
                        else
                            sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 登录号BETWEEN '" + sdlh + "' and '"+ edlh +"')";
                    }
                    sql += " order by 主键码 DESC";
                    _open.Reset();
                    _open.SqlCmd = sql;
                    this.gcdt = _open.GetDataTable();
                    if (this.gcdt.Rows.Count > 0)
                    {
                        using (ProgressHolder holder = new ProgressHolder(DoWork, "处理中..."))
                        {
                            holder.DoIt(this.gcdt.Rows.Count);
                        }
                        ShowMessageBox.ShowMessageBoxTimeout("成功更新" + sum + "条", "提示", MessageBoxButtons.OK, 3000);
                    }
                    else
                        ShowMessageBox.ShowMessageBoxTimeout("检索结果为空，请重试...", "提示", MessageBoxButtons.OK, 3000);
                }
            }
        }

        private void udqkBtn_Click(object sender, EventArgs e)
        {
            this.all = false;
            using (DbOperHandler _open = new Common().Doh())
            {
                string stime = string.Empty, etime = string.Empty;
                if (this.rcCheck.Checked)
                {
                    stime = this.sTime.Text.Trim();
                    etime = this.eTime.Text.Trim();
                }
                string sql = " select 主键码 from 馆藏书目库 where 库键码=" + this._kjm;
                if (!string.IsNullOrEmpty(stime))
                    sql += "AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and (入藏日期 BETWEEN '" + stime + "' AND '" + etime + "'))";
                if (this.ztKay.SelectedIndex == 1)
                    sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'G')";
                if (this.ztKay.SelectedIndex == 2)
                    sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' and 登录号 is not null)";
                if (this.ztKay.SelectedIndex == 3)
                    sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE 馆藏书目库.主键码 = 采购库.主键码 and 状态= 'C' and 登录号 is null)";
                if (this.ztKay.SelectedIndex == 4)
                    sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 状态= 'L')";
                string dlht = StringUtil.ToDBC(this.dlhTxt.Text.Trim());
                if (!string.IsNullOrEmpty(dlht))
                {
                    string sdlh = string.Empty, edlh = string.Empty;
                    if (dlht.Contains(","))
                    {
                        string[] dlhs = dlht.Split(',');
                        sdlh = dlhs[0];
                        edlh = dlhs[1];
                        if (sdlh.Length > edlh.Length)
                        {
                            edlh = sdlh.Substring(0, sdlh.Length - edlh.Length) + edlh;
                        }
                        //Console.WriteLine(sdlh + ":" + edlh);
                    }
                    else
                        sdlh = dlht;
                    if (string.IsNullOrEmpty(edlh))
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 登录号>='" + sdlh + "')";
                    else
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE 馆藏书目库.主键码 = 馆藏典藏库.主键码 and 登录号 BETWEEN '" + sdlh + "' and '" + edlh + "')";
                }
                if (this.xzssb.Equals("馆藏书目库"))
                    sql += " and " + bzd + " is null";
                if (this.xzssb.IndexOf("检索")>=0)
                    sql += " and not  EXISTS(select 主键码 from "+ this.xzssb+" where 类型="+this.lx+ " and 馆藏书目库.主键码="+ this.xzssb+".主键码)";
                sql += " order by 主键码 DESC";
                this.all = false;
                Console.WriteLine(sql);
                _open.Reset();
                _open.SqlCmd = sql;
                this.gcdt = _open.GetDataTable();
                if (this.gcdt.Rows.Count > 0)
                {
                    using (ProgressHolder holder = new ProgressHolder(DoWork, "处理中..."))
                    {
                        holder.DoIt(this.gcdt.Rows.Count);
                    }
                    ShowMessageBox.ShowMessageBoxTimeout("更新【" + this.bzd + "】完成，更新" + this.gcdt.Rows.Count + "条", "提示", MessageBoxButtons.OK, 3000);
                }
                else
                    ShowMessageBox.ShowMessageBoxTimeout("检索结果为空，请重试...", "提示", MessageBoxButtons.OK, 3000);
            }
        }
    } 
}
